package game.map;

import java.io.File;
import java.util.Random;

import processing.core.PApplet;
import processing.core.PImage;
import game.mechanics.Item;

public class MapItem {
	
	private Item item;
	private int x, y;
	private PImage img;
	private String imgPath;
	
	public MapItem(int itemId, int x, int y, String imgPath, PApplet app) {
	
		super();
		this.imgPath = imgPath;
		this.x = x;
		this.y = y;
		
		item = Item.getItemByID(itemId); // TODO item? equip? weapon?
		File file = new File ("img/" + imgPath);
		if (file.exists()) {
			img = app.loadImage("img/" + imgPath);
		} else {
			img = app.loadImage("img/chest.png");
		}
		
	}
	
	public MapItem(int itemId, int x, int y, String imgPath, long mapItemId, 
			PApplet app) {
		this(itemId, x, y, imgPath, app);
		// TODO imgId
	}
	
	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Item getItem() {
		return item;
	}
	
	public void draw(PApplet app, int side) {
		app.image(img, getPosX(side), getPosY(side)); 
	}

	public void drawGlow(PApplet app, int side) {
		app.noFill();
		app.strokeWeight(1);
		app.color(0xFFFFFFFF);
		app.rect(getPosX(side), getPosY(side), img.width, img.height);
	}

	public int getImgWidth() {
		return img.width;
	}
	
	public int getImgHeight() {
		return img.height;
	}
	
	public boolean mouseOverImg(int mx, int my, int side) {
		return getPosX(side) <= mx && mx < getPosX(side) + getImgWidth() &&
				getPosY(side) <= my && my < getPosY(side) + getImgHeight();
	}
	
	private int getPosX(int side) {
		return getX()*side + (side - img.width)/2;
	}
	
	private int getPosY(int side) {
		return getY()*side + (side - img.height)/2;
	}
	
	protected long id;
	
	public void setId() {
		Random random = new Random();
		this.id = random.nextLong();
	}

	public long getId() {
		return id;
	}

	public String getImgPath() {
		return imgPath;
	}
	
}
